import * as types from './mutation-types'
import * as constant from '../common/constant'
import axios from 'axios'
import Vue from 'vue'

axios.defaults.crossDomain = true;
axios.defaults.withCredentials=true;

function initUser(state) {
  if (localStorage.user) {
    state.user = JSON.parse(localStorage.user);
  } else {
    if (window.location.hash != "#/login") {
      window.location.href = "/#/login";
    }
  }
}

function saveUser(state) {
  if (state.response.code == '200') {
    delete state.response.data['password'];
    localStorage.user = JSON.stringify(state.response.data);
    
    window.location.href = "/";
  } else {
    localStorage.removeItem('user');
  }
}

export default {
  [types.LOGIN_AUTH] (state, req) {
    var user = req.user;
    axios.post(constant.API_SERVER + "/loginPost", user).then(response => {
      state.response = response.data;
      saveUser(state);
      state.loading = false;
        if (req.callafter) {
          (req.callafter)(state.response);
        }
        if (state.response.code != '200') {
          if (req.callback) {
            (req.callback)();
          }
        }
      }, response => {
        state.loading = false;
        if (req.callafter) {
          (req.callafter)(state.response);
        }
        if (req.callback) {
          (req.callback)();
        }
      }
    );
  },
  [types.USER_TOKEN] (state, req) {
   
    axios.post(constant.API_SERVER + "/loginPost", req).then(response => {
        if (response.data && response.data.loginName) {
            if (req.callback) {
                (req.callback)(response.data);
              }
        }
      }, response => {
      }
    );
  },
  [types.INSTALL_AUTH](state) {
    axios.post(constant.API_SERVER + "/installAuth").then(response => {
      state.loading = false;
      if (true !== response.data) {
        window.location.href = "/";
      }
    }, response => {
      state.loading = false;
    }
    );
  },
  [types.INSTALL](state, req) {
    axios.post(constant.API_SERVER + "/install", req.user).then(response => {
      state.loading = false;
      if (req.callafter) {
        (req.callafter)(response.data);
      }
    }, response => {
      state.loading = false;
    }
    );
  },
  [types.INIT_USER_FROM_STORE] (state) {
    initUser(state);
  },
  [types.LOGOUT_USER] (state) {
    axios.post(constant.API_SERVER + "/logout").then(response => {
        window.location.href = "/#/login";
        //window.location.reload();
      }, response => {
      }
    );
  },
  [types.LOAD_MESSAGES] (state, req) {
    axios.get(constant.API_SERVER + "/loadLocal?lang=" + req.lang).then(response => {
        localStorage.messages = JSON.stringify(response.data);
        if (req.callafter) {
          (req.callafter)(state.response);
        }
      }, response => {
      }
    );
  },
  [types.REFRESH_USER] (state, user) {
    Vue.set(state, 'user', user);
    localStorage.user = JSON.stringify(state.user);

    axios({
      method: 'post',
      url: constant.API_SERVER + '/refreshUser',
      data: user,
    }).then(response => {
      }, error => {
      }
    );

  },
  [types.REMEMBER_PASSWORD](state, req) {
    var data = req['user'];
    axios({
        method: 'post',
        url: constant.API_SERVER + '/loginInfo',
        data: data,
    }).then(response => {
        if (req.callback) {
          (req.callback)(response.data);
        }
      }, error => {

      }
    );
  },
};
